package adapter;

import java.lang.reflect.Array;
import java.security.spec.InvalidParameterSpecException;
import java.util.ArrayList;
import java.util.List;

import com.example.remotecontrol.R;
import common.DimensionHelper;
import components.AlarmManager;
import components.AlertDialogManager;

import controller.PasscodeFragment;
import data.RCDevice;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.TwoLineListItem;


public class OptionAdapter extends ArrayAdapter<String[]>{

	private Context context = null;
	private List<RCDevice> mListItem = null;
	private int mResourceID = 0;
	public OptionAdapter(Context context,  List<RCDevice> objects) {
		super(context, R.layout.item_option);
		
		this.context = context;
		mListItem = objects;
		mResourceID = R.layout.item_option;
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mListItem == null) {
			return 0;
		}
		return mListItem.size();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getApplicationContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row =  inflater.inflate(
					this.mResourceID, null);
		} else {
			row = convertView;
		}
		final RCDevice item = mListItem.get(position);

		TextView textView = (TextView) row.findViewById(R.id.item_option_name);
		ImageView alertImage = (ImageView) row.findViewById(R.id.item_options_img_btn_alarm);
		textView.setText(item.getName());
		textView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final Dialog dialog = new Dialog(context);
				dialog.setContentView(R.layout.dialog_change_name);
				dialog.setTitle(context.getString(R.string.alert_title));

				// set the custom dialog components - text, image and button
				// TextView text = (TextView) dialog.findViewById(R.id.text);
				// text.setText("Android custom dialog example!");
				// ImageView image = (ImageView) dialog.findViewById(R.id.image);
				// image.setImageResource(R.drawable.ic_launcher);
				Button cancleDialogButton = (Button) dialog.findViewById(R.id.btn_cancel);
				cancleDialogButton.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				
				Button dialogButton = (Button) dialog.findViewById(R.id.btn_OK);
				// if button is clicked, close the custom dialog
				dialogButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// CheckBox chk = (CheckBox)
						// dialog.findViewById(R.id.chk_not_show_again);
						// if (chk.isChecked()) {
						// SharePreferencesManager.dontShowPowerConfirm(context);
						// }

						EditText editText = (EditText) dialog.findViewById(R.id.item_edit_new_device_name);
						String newName = editText.getText().toString();
						
						if (newName.isEmpty()) {
							return;
						}
						
						try {
							AlarmManager.sharedAlarmList(getContext()).changeDeviceName(item, newName);
						} catch (InvalidParameterSpecException e) {
							// TODO Auto-generated catch block
							AlertDialogManager.createAlertDialog(getContext(),
									context.getString(R.string.alert_title), e.getMessage(), context.getString(R.string.OK)
									, new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											dialog.dismiss();
										}
									}).show();
						}
						OptionAdapter.this.notifyDataSetInvalidated();
						dialog.dismiss();
					}
				});

				Button cancelButton = (Button) dialog.findViewById(R.id.btn_cancel);
				cancelButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
				dialog.show();
			}
		});
		
		if (item.isAlert()) {
			//TODO : show alert
//			alertImage.setBackgroundResource(R.drawable.alarm_alert_state);
			
			
//			alertImage.getLayoutParams().height = 50;
//			alertImage.getLayoutParams().width = 50;
//			alertImage.refreshDrawableState();
			
			RelativeLayout.LayoutParams params = (LayoutParams) alertImage.getLayoutParams();
			
			int px = DimensionHelper.getPxFromDp(context, 50);
			params.width =  px;
			params.height = px;
			
			params.rightMargin = DimensionHelper.getPxFromDp(context, 11);
			alertImage.setLayoutParams(params);
			
			
//			ImageView.LayoutParams params = new ImageView.LayoutParams(50,50);
//			alertImage.setLayoutParams(params);
			if (alertImage.getAnimation() == null || alertImage.getAnimation().hasEnded())
            {
				AnimationDrawable ani = (AnimationDrawable) context.getResources().getDrawable(R.drawable.alert_alarm);
				alertImage.setImageDrawable(ani);
				ani.start();
            }
		} else {
//			alertImage.setBackgroundResource(R.drawable.alarm_normal_state);
			RelativeLayout.LayoutParams params = (LayoutParams) alertImage.getLayoutParams();
			int px = DimensionHelper.getPxFromDp(context, 32);
			params.width =  px;
			params.height = px;
			params.rightMargin = DimensionHelper.getPxFromDp(context, 20);
			alertImage.setLayoutParams(params);
			
			alertImage.setImageResource(R.drawable.bell_normal);
		}

		return row;
	}

}
